package com.yunsuo.bean;

import java.lang.management.LockInfo;
import java.sql.*;
import java.util.concurrent.locks.Lock;

public class DoorStatus {

    public static Connection getConn() {
        String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/raspberry?characterEncoding=UTF-8";
        String username = "root";
        String password = "123456";
        Connection conn = null;
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static LockRaspberry getAll() {
        LockRaspberry lockRaspberry = new LockRaspberry(null, null, null, null, null,
                null, null, null, null, null);
        Connection conn = getConn();
        String sql = "select * from lock_raspberry";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();
            //System.out.println("============================");
            while (rs.next()) {
                lockRaspberry.setlId(Integer.parseInt(rs.getString("l_id")));
                lockRaspberry.setlUuid(rs.getString("l_uuid"));
                lockRaspberry.setlStatus(Integer.parseInt(rs.getString("l_status")));
                lockRaspberry.setlModify(Integer.parseInt(rs.getString("l_modify")));
            }
            //System.out.println(lockRaspberry);
            //System.out.println("============================");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return lockRaspberry;
    }

    //把门锁状态修改为未被修改的状态
    public static int update(int l_id) {
        Connection conn = getConn();
        int i = 0;
        String sql = "update lock_raspberry set l_modify='" + 0 + "' where l_id='" + l_id + "'";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            i = pstmt.executeUpdate();
            //System.out.println("resutl: " + i);
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
}
